package com.assignwork.service.impl;

import com.assignwork.dto.ShiftRuleTimeSpaceSubmitDTO;
import com.assignwork.entity.ShiftRuleDailyShift;
import com.assignwork.entity.ShiftRuleTimeSpace;
import com.assignwork.mapper.ShiftRuleTimeSpaceMapper;
import com.assignwork.service.IShiftRuleTimeSpaceService;
import com.assignwork.vo.ShiftRuleTimeSpaceVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName ShiftRuleTimeSpaceServiceImpl
 * @Description 班组生成规则时间段信息
 * @Author dujianyu
 * @Date 2023/12/1 16:08
 */
@Service
public class ShiftRuleTimeSpaceServiceImpl extends BaseServiceImpl<ShiftRuleTimeSpaceMapper, ShiftRuleTimeSpace> implements IShiftRuleTimeSpaceService {

    @Override
    public boolean removeSRTimeSpacesByRuId(Long ruleId) {
        QueryWrapper<ShiftRuleTimeSpace> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .select(ShiftRuleTimeSpace::getId)
                .eq(ShiftRuleTimeSpace::getIsDeleted , 0)
                .eq(ShiftRuleTimeSpace::getRuleId, ruleId);
        List<ShiftRuleTimeSpace> sRTimeSpacesByRuleId = baseMapper.selectList(queryWrapper);
        if (Func.isEmpty(sRTimeSpacesByRuleId)) {
            return true;
        } else {
            List<Long> ids = sRTimeSpacesByRuleId.stream().map(ShiftRuleTimeSpace::getId).collect(Collectors.toList());
            return super.deleteLogic(ids);
        }
    }

    @Override
    public boolean submitSRTimeSpaceBatch(List<ShiftRuleTimeSpaceSubmitDTO> timeSpaces) {
        List<ShiftRuleTimeSpace> entityList = new ArrayList<>();
        for (ShiftRuleTimeSpaceSubmitDTO timeSpace : timeSpaces) {
            ShiftRuleTimeSpace entity = new ShiftRuleTimeSpace();
            entity.setId(timeSpace.getId());
            entity.setRuleId(timeSpace.getRuleId());
            entity.setCode(timeSpace.getCode());
            entity.setTimeSpace(timeSpace.getTimeSpace());
            entityList.add(entity);
        }

        if (Func.isEmpty(entityList)) {
            return true;
        } else {
            return super.saveOrUpdateBatch(entityList);
        }

    }

    @Override
    public List<ShiftRuleTimeSpaceVO> selectSRTimeSpaceListByRuleId(String ruleId) {
        QueryWrapper<ShiftRuleTimeSpace> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ShiftRuleTimeSpace::getRuleId, Func.toLong(ruleId)).eq(ShiftRuleTimeSpace::getIsDeleted, 0).orderByAsc(ShiftRuleTimeSpace::getCode);
        List<ShiftRuleTimeSpace> shiftRuleTimeSpaces = baseMapper.selectList(queryWrapper);
        if (Func.isEmpty(shiftRuleTimeSpaces)) {
            throw new RuntimeException("该规则没有时间段信息");
        }
        return BeanUtil.copy(shiftRuleTimeSpaces, ShiftRuleTimeSpaceVO.class);
    }

}
